<template>
	<oauth-loading oauthIcon="wechat"></oauth-loading>
</template>

<script>
import Tools from '@/utils/Tools.js';
import oauthLoading from '@/components/oauth/loading.vue';
import { updateUserToken, updateRefereeCode } from '@/store/dispatches.js';
import { mapGetters } from 'vuex';
import { initIMService } from '@/store/dispatches.js';
export default {
	components: { oauthLoading },
	data() {
		return {
			options: {},
			code: '',
			authed: false,
			bindStatus: null,
			authId: null,
			referee_id: ''
		};
	},
	onLoad(options) {
		this.options = options;
		this.code = options.code;
		this.$nextTick(() => {
			this.codeToAuthId();
		});
		console.log(this.refereeCode)
	},
	computed: {
		...mapGetters({
			refereeCode: 'getRefereeCode'
		})
	},
	methods: {
		//
		codeToAuthId() {
			this.$u.api.loginByWechat(this.code, this.refereeCode).then(res => {
				this.bindStatus = res.data.code;
				this.authId = res.data.id;
				this.referee_id = res.data.referee_id;
				this.$nextTick(() => {
					this.authForLogin();
				});
			});
		},
		// 微信授权
		authForLogin() {
			if (this.bindStatus == 1) {
				this.$u.api.loginByAuthId(this.authId, this.refereeCode).then(res => {
					updateRefereeCode('');
					initIMService();
					updateUserToken(res.data.token);
					this.refreshUserInfo();
				});
			} else {
				this.redirectTo({ url: '/pageUser/login/bind?auth_id=' + this.authId });
			}
		},
		completeUserInfo() {
			this.reLaunch();
		}
	}
};
</script>

<style></style>
